package com.example.demo;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.*;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2023-10-24
 * Time:20:46
 * Text：SpringBoot的创建
 */
@RequestMapping("/param")
@RestController
public class HelloSpring {
    @RequestMapping("/Hi")
    public String sayHi(String name){

        return "Hello,SpringBoot";
    }

    //参数传递，使用包装类型，可不需要传值，但是基本类型需要参数
    @RequestMapping("/m2")
    public String saymessage(String name,Integer age){

        return "姓名："+name+"年龄："+age;
    }

    //参数重命名,必传参数
    @RequestMapping("/m3")
    public String sayname(@RequestParam("name")String username){

        return "姓名："+username;
    }

    //参数重命名,修改required,非必传参数
    @RequestMapping("/m4")
    public String m4(@RequestParam(value="name",required = false)String username){

        return "姓名："+username;
    }

    //传递数组
    @RequestMapping("/m5")
    public String m5(String[] array){

        return "接收到的数组："+ Arrays.toString(array);
    }

    //传递过来的是数组，需要使用@RequestParam改为列表
    @RequestMapping("/m6")
    public String m6(@RequestParam List<String> array){

        return "接收到的数组："+ array+"长度："+array.size();
    }

    //传递json对象
    @RequestMapping("/m7")
    public String m7(@RequestBody Person person){

        return "接收的person："+person.toString();
    }

    //返回url的参数
    @RequestMapping("/m8/{userId}")
    public String m8(@PathVariable Integer userId){

        return "接收的person："+userId;
    }

    //返回url的参数
    @RequestMapping("/m9")
    public String m9(@RequestPart MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("路径"));
        return "success";
    }

    //servlet传统获取cookie
    @RequestMapping("/m10")
    public String getCookie(HttpServletRequest request){
        Cookie[] array=request.getCookies();
        if(array!=null){
            Arrays.stream(array).forEach(i->{
                System.out.println(i.getName()+" "+i.getValue());
            });
        }

//        for(Cookie cookie:array){
//            System.out.println(cookie);
//        }
        return "获取成功";
    }

//    Spring获取cookie,COOKIE的value值
@RequestMapping("/m11")
public String getCookie2(@CookieValue String TQ02){

        return "获取成功"+TQ02;
    }



//    设置Session
@RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        HttpSession session=request.getSession();
        session.setAttribute("username","zhangsan");
        return "success";
    }

//    获取session
@RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        HttpSession session= request.getSession(false);
        if(session!=null){
            String username=(String)session.getAttribute("username");
            return "session"+username;
        }
        return "session为空";
    }



    //    获取session
    @RequestMapping("/getSession2")
    public String getSession2(@SessionAttribute(required = false) String username){

        return "username"+username;
    }
//    通过Spring内置函数获取Session
    @RequestMapping("/getSession3")
    public String getSeesion(HttpSession session){
        String name=(String)session.getAttribute("username");
        return "登陆用户"+name;
    }

//    传统方法获取头
@RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String str1=request.getHeader("User-Agent");
        return "user"+str1;
    }
    //    Spring获取头
    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent")String userAgent){
        return "user"+userAgent ;
    }

}

